<?
class tsearch {
    const SEARCH_API = 'http://search.twitter.com';
    const DOCOMPRESS = false;

    public function tsearch() {
        $this->check_server();
        $this->method = $_SERVER['REQUEST_METHOD'];
        $this->search_api = self::SEARCH_API;
        $this->docompress = self::DOCOMPRESS;
		
        $this->pre_request();
        $this->dorequest();
        $this->post_request();
    }

    private function pre_request() {
		if(strrchr($_SERVER['REQUEST_URI'], '?') !== false){
			$this->request_api = strval(substr(strrchr($_SERVER['REQUEST_URI'], '?'), 0));
		}else{
			$this->request_api = '';
		}

        $this->request_api = $this->mytrim($this->request_api);
        if($this->method == 'POST'){
            $this->post_data = @file_get_contents('php://input');
        }
    }

    private function dorequest() {
		$url = $this->search_api.'/';
		if (!empty($this->request_api)) {
			$url .= 'search' . $this->request_api;
		}
        $ch = curl_init($url);
        $curl_opt = array();
        if($this->method == 'POST') {
            $curl_opt[CURLOPT_POST] = true;
            $curl_opt[CURLOPT_POSTFIELDS] = $this->post_data;
        }
        $curl_opt[CURLOPT_USERAGENT] = $_SERVER['HTTP_USER_AGENT'];
        $curl_opt[CURLOPT_RETURNTRANSFER] = true;
        $curl_opt[CURLOPT_HEADERFUNCTION] = create_function('$ch,$str','if(strpos($str,\'Content-Length:\') === false ) { header($str); } return strlen($str);');
        $curl_opt[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_1 ;//avoid the "Expect: 100-continue" error
        curl_setopt_array($ch,$curl_opt);
        $this->ret = curl_exec($ch);
        curl_close($ch);
    }
	
    private function post_request() {
		$result = str_replace('/images/search/', '', $this->ret);
		$result = str_replace('/stylesheets/search/', '', $result);
		$result = str_replace('/javascripts/search/', '', $result);
		$result = str_replace('http://search.twitter.com/search?', 'search.php?', $result);
	    $result = str_replace('/search?', 'search.php?', $result);
	    $result = str_replace('"/search"', '"search.php"', $result);
        header('Content-Length: '.strlen($result));

        if($this->docompress && Extension_Loaded('zlib')) {
            if(!Ob_Start('ob_gzhandler')) {
                Ob_Start();
            }
        }

        echo $result;

        if($this->docompress && Extension_Loaded('zlib')) {
            Ob_End_Flush();
        }
    }

    private function mytrim($str) {
        return trim($str,'/');
    }
	
    private function check_server() {
        if(!function_exists('curl_init') &&
        !function_exists('curl_setopt_array') &&
        !function_exists('curl_exec') &&
        !function_exists('curl_close')) {
            $this->err("curl functions doesn't exists!");
        }
        else if(!function_exists('file_get_contents') && !function_exists('file_put_contents')) {
            $this->err("PHP 5 is needed!");
        }
    }
	
    private function err($str=null) {
        if(empty($str)) {
            $str = 'Seems every thing is fine.';
        }
        $msg ="
                <html>
                <head>
                <title>Twitter Search Message Page</title>
                </head>
                <body>
                <h1>Twitter Search Message Page</h1>
                <div>
                This is a Twitter Search proxy, and there is something wrong.<br />
                </div>
                <div>
                ".nl2br($str)."
                </div>
                </body>
                </html>
                ";
        echo $msg;
        exit();
    }
}

$tsearch = new tsearch();
?>